package com.action;

import com.hdb.DB;
import com.hdb.TUser;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@ParentPackage("default")
@Namespace("/user")
@Results({
        @Result(location = "/admin/user/userManage.jsp"),
        @Result(name = "succeed", location = "/common/succeed.jsp"),
        @Result(name = "userEditPage", location = "/admin/user/userEditPage.jsp"),
        @Result(name = "search", location = "/admin/user/userSearch.jsp"),
        @Result(name = "userInfo", location = "/admin/user/userInfo.jsp"),
})
@InterceptorRef("adminStack")/*设置过滤器*/
public class AdminUserAction extends BasicAction {
    private int userId;
    private String userName;
    private String userPw;
    private String userRealName;
    private int userSex;
    private String userTel;
    private String userEmail;
    private String userQq;
    private int page;
    private String newUserPw;
    private int searchType;
    private String searchInfo;
    private String message;
    private String path;

    private DB db;
    /*管理员修改用户信息*/
    @Action("userEdit")
    public String userEdit() {
        TUser user = db.userDAO.findById(userId);
        if (user == null) {
            setMessage(getResultInfo(false));
            setPath("user/userManage.action");
            return "succeed";
        }
        user.setUserName(userName);
        if (userPw.trim().length() >= 6) {
            user.setUserPw(userPw);
        }
        user.setUserTel(userTel);
        user.setUserEmail(userEmail);
        user.setUserSex(userSex);
        user.setUserQq(userQq);
        setMessage(getResultInfo(
                db.userDAO.attach(user)
        ));
        setPath("user/userManage.action");
        return "succeed";
    }
    /*查看用户信息*/
    @Action("userInfo")
    public String userInfo() {
        TUser user = db.userDAO.findById(userId);
        getRequest().put("user", user);
        return "userInfo";
    }
/*    *//*用户修改页面*//*
    @Action("userEditPage")
    public String userEditPage() {
        TUser user = db.userDAO.findById(userId);
        getRequest().put("user", user);
        return "userEditPage";
    }*/
    /*添加用户*/
    @Action("userAdd")
    public String userAdd() {
        if (userPw.trim().length() < 6) {
            setMessage("密码至少6位");
            setPath("admin/user/userAdd.jsp");
            return "succeed";
        }
        TUser user = new TUser();
        user.setUserName(userName);
        user.setUserPw(userPw);
        user.setUserRealName(userRealName);
        user.setUserEmail(userEmail);
        user.setUserQq(userQq);
        user.setUserTel(userTel);
        user.setUserSex(userSex);
        this.setMessage(getResultInfo(
                db.userDAO.save(user)
        ));
        this.setPath("user/userManage.action");
        return "succeed";
    }
    /*删除用户*/
    @Action("userDel")
    public String userDel() {
        TUser user = db.userDAO.findById(userId);
        db.userDAO.delete(user);
        this.setMessage("删除成功");
        this.setPath("user/userManage.action");
        return "succeed";
    }
    /*用户管理*/
    @Action("userManage")
    public String userManage() {
        int[] p = getP();
        List userList = db.userDAO.findAll(p[0], p[1]);
        Map request = (Map) ServletActionContext.getContext().get("request");
        request.put("userList", userList);
        getPLink(userList.size());
        return ActionSupport.SUCCESS;
    }
    /*修改用户页面*/
    @Action("userEditPage")
    public String userEditPage() {
        TUser user = db.userDAO.findById(userId);
        if (user == null) {
            this.setMessage("对象不存在");
            this.setPath("user/userManage.action");
            return "succeed";
        }
        getRequest().put("user", user);
        return "userEditPage";
    }
    /*搜索用户*/
    @Action("userSearch")
    public String userSearch() {
        Map request = (Map) ServletActionContext.getContext().get("request");
        List userList;
        if (searchInfo == null) {
            userList = new ArrayList();
            request.put("userList", userList);
            return "search";
        }
        int[] p = getP();
        String type;
        switch (searchType) {
            case 0:
                type = "userName";
                break;
            case 1:
                type = "userEmail";
                break;
            case 2:
                type = "userTel";
                break;
            case 3:
                type = "userRealName";
                break;
            case 4:
                type = "userQq";
                break;
            default:
                type = "userName";
                break;
        }
        userList = db.userDAO.search(type, searchInfo.trim(), p[0], p[1]);
        request.put("userList", userList);
        getPLink(userList.size());
        return "search";
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getSearchType() {
        return searchType;
    }

    public void setSearchType(int searchType) {
        this.searchType = searchType;
    }

    public String getSearchInfo() {
        return searchInfo;
    }

    public void setSearchInfo(String searchInfo) {
        this.searchInfo = searchInfo;
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserPw() {
        return userPw;
    }

    public void setUserPw(String userPw) {
        this.userPw = userPw;
    }

    public String getUserRealName() {
        return userRealName;
    }

    public void setUserRealName(String userRealName) {
        this.userRealName = userRealName;
    }

    public int getUserSex() {
        return userSex;
    }

    public void setUserSex(int userSex) {
        this.userSex = userSex;
    }

    public String getUserTel() {
        return userTel;
    }

    public void setUserTel(String userTel) {
        this.userTel = userTel;
    }

    public String getUserEmail() {
        return userEmail;
    }

    public void setUserEmail(String userEmail) {
        this.userEmail = userEmail;
    }

    public String getUserQq() {
        return userQq;
    }

    public void setUserQq(String userQq) {
        this.userQq = userQq;
    }

    public String getNewUserPw() {
        return newUserPw;
    }

    public void setNewUserPw(String newUserPw) {
        this.newUserPw = newUserPw;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public DB getDb() {
        return db;
    }

    public void setDb(DB db) {
        this.db = db;
    }
}
